<?php

require_once('../../connection/_dbconnect_.php');
require_once('m_activity_log.php');

function getRole($id){

	$fetchedRole = array();
	
	$sql = "select idrole,name from role where idrole='".$id."'";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	while($obj = mysql_fetch_object($result)){
		$fetchedRole[] = $obj->idrole;
		$fetchedRole[] = $obj->name;
	}
	
	return $fetchedRole;
}

function getRoles(){
	
	$parentArray = array();
	
	$sql = "select idrole,name from role";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	while($obj = mysql_fetch_object($result)){
		$childArray = array();		
		$childArray[] = $obj->idrole;
		$childArray[] = $obj->name;
		
		$parentArray[]= $childArray;
	}

	return $parentArray;
}

function addRole($newRole){
	
	$sql = "insert into role (idrole, name) values (NULL, '".$newRole."');";
	
	$result = mysql_query($sql);
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "Role=".$newRole." was created");
		return true;
	}
}

function saveRoleEdit($idrole, $new_name){
	
	$sql = "update role set name='".$new_name."' where idrole='".$idrole."';";
	
	$result = mysql_query($sql);
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "Role=".$idrole." was modified");
		return true;
	}
}

function deleteRole($idrole){
	
	$sql = "delete from role where idrole='".$idrole."';";
	
	$result = mysql_query($sql);
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "Role=".$idrole." was deleted");
		return true;
	}
}



?>